package com.credithc.signature.common.utils;

import com.credithc.signature.common.ro.manager.TemplateRo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.Calendar;

@Component
public class GeneralUtil {

    private static final String SYSTEMID = "QZ";
    private static GeneralUtil obj = new GeneralUtil();

    /**
     * 生成模板编号   生成规则: 模板编号= QZ + 年份 + 产品别名 + 五位编号（从1开始，不够前补0）
     *
     * @param templateNo
     * @param channelAlias
     * @return
     */
    public static String buildTemplateNo(String templateNo, String channelAlias) {
        //兼容老系统模板编号
        if (templateNo == null || templateNo.length() <= 6) {
            return getLatestNo(null, SYSTEMID, channelAlias);
        } else {
            String latestNo = templateNo.substring(templateNo.length() - 5);
            return getLatestNo(latestNo, SYSTEMID, channelAlias);
        }
    }

    /**
     * 根据Redis生成模板编号
     *
     * @param
     * @param channelAlias
     * @return
     */
    public static String buildTemplateNoPrefix(String channelAlias) {
        //获取当前年份
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return SYSTEMID + year + channelAlias;
    }

    /**
     * 生成渠道编号   生成规则: 模板编号= 年份 + 五位编号（从1开始，不够前补0）
     *
     * @param channelNo
     * @param channelAlias
     * @return
     */
    public static String buildChannelNo(String channelNo, String channelAlias) {
        if (channelNo == null) {
            return getLatestNo(null, null, channelAlias);
        } else {
            String latestNo = channelNo.substring(channelNo.length() - 5);
            return getLatestNo(latestNo, null, channelAlias);
        }
    }

    /**
     * 生成编号
     *
     * @param demandNo
     * @param systemId
     * @param bizId
     * @return
     */
    private static String getLatestNo(String demandNo, String systemId, String bizId) {
        //1.初始编号
        String newDemandNo = "00001";
        //2.获取当前年份
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        //3.生成规则前缀
        String prefix;
        if (systemId != null) {
            prefix = systemId + year + (bizId == null ? "" : bizId);
        } else {
            prefix = (bizId == null ? "" : bizId) + year;
        }
        //4.生成规则编号
        if (demandNo == null) {
            newDemandNo = prefix + newDemandNo;
        } else {
            String newTemplate = String.format("%05d", Integer.parseInt(demandNo) + 1);
            newDemandNo = prefix + newTemplate;
        }
        return newDemandNo;
    }

    /**
     * 自动生成版本号：当前版本+1
     *
     * @param templateRo
     * @return
     */
    public static String buildVersion(TemplateRo templateRo) {
        synchronized (obj) {
            String version = templateRo.getVersion();
            version = version.substring(version.indexOf("_") + 1, version.indexOf("."));
            String newTemplateNo = "1";
            String prefix = "V_";
            String Suffix = ".0";
            if (StringUtils.isEmpty(version)) {
                newTemplateNo = prefix + newTemplateNo + Suffix;
            } else {
                String newTemplate = String.valueOf(Integer.parseInt(version) + 1);
                newTemplateNo = prefix + newTemplate + Suffix;
            }
            return newTemplateNo;
        }
    }

    /**
     * 获取文件大小
     *
     * @param fileLength
     * @return
     */
    public static String fileSize(long fileLength) {
        String size = "";
        DecimalFormat df = new DecimalFormat("#.00");
        if (fileLength == 0) {
            size = "0KB";
        } else if (fileLength > 0 && fileLength < 1024) {
            size = "1KB";
        } else if (fileLength < 1048576) {
            size = df.format((double) fileLength / 1024) + "KB";
        } else if (fileLength < 1073741824) {
            size = df.format((double) fileLength / 1048576) + "MB";
        } else {
            size = df.format((double) fileLength / 1073741824) + "GB";
        }
        return size;
    }
}
